/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view.component.doublelist;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import org.opu.vcforms.bean.model.DoubleListModel;
import org.opu.vcforms.bean.model.DoubleListModel.GUIListModel;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.resource.ResourceHandle.IconType;
import org.opu.vcforms.view.View;
import org.opu.vcforms.view.component.GUIProperties;

/**
 *
 * @author yura
 */
public class JDoubleList extends JComponent implements View{
    /** Move all selected element from left list to right button (action, icon) name*/
    public static final String ALLTOLEFT    = "allToLeft";

    /** Move all selected element from right list to left button (action, icon) name*/
    public static final String ALLTORIGHT   = "allToRight";

    /** Move all all element from left list to right button (action, icon) name*/
    public static final String TOLEFT       = "toLeft";

    /** Move all all element from right list to left button (action, icon) name*/
    public static final String TORIGHT      = "toRight";

    private GUIListModel leftModel;
    private GUIListModel rightModel;
    private JListImpl leftList;
    private JListImpl rightList;

    /** {@code <-} */
    private JButton toLeft;
    /** {@code ->} */
    private JButton toRight;
    /** {@code <<-} */
    private JButton allToLeft;
    /** {@code ->>} */
    private JButton allToRight;

    private JPanel leftPane;
    private JPanel centerPane;
    private JPanel rightPane;

    private DoubleListI18n i18n;
    private DoubleListModel model;

    public JDoubleList(DoubleListModel model){
        this(new DefaultDoubleListI18n(), model);
    }

    public JDoubleList(DoubleListI18n i18n, DoubleListModel model) {
        this.i18n = i18n;
        this.model = model;
        setLayout(new GridLayout(1, 3, 2, 2));
        this.initComponent();
    }

    @Override//hope it`s called not often
    public void reinit() {
        removeAll();
        initComponent();
    }

    private void initComponent(){
        leftModel = model.getAvailableGroupListModel();
        rightModel = model.getSelectedGroupListModel();
        
        IconType iconType = GUIProperties.instance().pane.button.getIconType();
        String png = ".png";

        Icon lftIcon = ResourceHandle.getIcon(TOLEFT + png, iconType);
        Icon rghtIcon = ResourceHandle.getIcon(TORIGHT + png, iconType);
        Icon allLftIcon = ResourceHandle.getIcon(ALLTOLEFT + png, iconType);
        Icon allRghtIcon = ResourceHandle.getIcon(ALLTORIGHT + png, iconType);

        leftList = new JListImpl(leftModel);
        rightList = new JListImpl(rightModel);
        resetData();

        Action mla = new MoveAction(rightList, leftList, lftIcon);//<-
        Action mra = new MoveAction(leftList, rightList, rghtIcon);//->
        Action mola = new MoveAllAction(rightList, leftList, allLftIcon);//<<-
        Action mora = new MoveAllAction(leftList, rightList, allRghtIcon);//->>

        leftList.setAction(mra);
        rightList.setAction(mla);

        toLeft = new JButton(mla);
        toRight = new JButton(mra);
        allToLeft = new JButton(mola);
        allToRight = new JButton(mora);

        GUIProperties guiProp = GUIProperties.instance();
        guiProp.pane.button.makeButtonFilled(toLeft);
        guiProp.pane.button.makeButtonFilled(toRight);
        guiProp.pane.button.makeButtonFilled(allToLeft);
        guiProp.pane.button.makeButtonFilled(allToRight);

        toLeft.setToolTipText(i18n.getMoveLftBtnTitle());
        toRight.setToolTipText(i18n.getMoveRgtBtnTitle());
        allToLeft.setToolTipText(i18n.getMoveAllLftBtnTitle());
        allToRight.setToolTipText(i18n.getMoveAllRgtBtnTitle());
        toLeft.setName(TOLEFT);
        toRight.setName(TORIGHT);
        allToLeft.setName(ALLTOLEFT);
        allToRight.setName(ALLTORIGHT);

        this.initButtnSize();

        String leftTitle = i18n.getLeftPaneTitle();
        String rightTitle = i18n.getRightPaneTitle();

        leftPane = new JPanel(new BorderLayout());
        centerPane = new JPanel(new BorderLayout());
        rightPane = new JPanel(new BorderLayout());

        int top = 10, below = 10;
        if (guiProp.pane.isLabel()) {
            leftPane.setBorder(createLineBorder(leftTitle));
            rightPane.setBorder(createLineBorder(rightTitle));
            top += 20;
        }

        leftPane.add(new JScrollPane(leftList), BorderLayout.CENTER);
        rightPane.add(new JScrollPane(rightList), BorderLayout.CENTER);

        Box V = Box.createVerticalBox();
        V.add(Box.createVerticalStrut(top));
        V.add(this.getComponentInHBox(toRight));
        V.add(this.getComponentInHBox(toLeft));
        V.add(this.getComponentInHBox(allToRight));
        V.add(this.getComponentInHBox(allToLeft));
        V.add(Box.createVerticalStrut(below));
        centerPane.add(V, BorderLayout.CENTER);
        centerPane.setMaximumSize(componentSize(centerPane));
        
        add(leftPane);
        add(centerPane);
        add(rightPane);
    }

    protected TitledBorder createLineBorder(String leftTitle) {
    //    if (leftTitle != null) {
            Border lb = BorderFactory.createLineBorder(Color.BLACK);
            return BorderFactory.createTitledBorder(lb, leftTitle);
    //    } else {
    //        return null;
    //    }
    }

    @Override
    public void resetData() {
    }

    private void initButtnSize() {
        Dimension s = new Dimension(componentSize(toLeft));
        s = compare(componentSize(toRight), s);
        s = compare(componentSize(allToLeft), s);
        s = compare(componentSize(allToRight), s);

        toLeft.setMaximumSize(s);
        toRight.setMaximumSize(s);
        allToLeft.setMaximumSize(s);
        allToRight.setMaximumSize(s);
    }

    protected Dimension componentSize(JComponent comp){
        return comp.getPreferredSize();
    }

    protected Dimension compare(Dimension d, Dimension s) {
        if (d.width > s.width) {
            s.width = d.width;
        }
        if (d.height > s.height) {
            s.height = d.height;
        }
        return s;
    }

    protected JComponent getComponentInHBox(JComponent comp){
        Box b = Box.createHorizontalBox();
        b.add(Box.createGlue());
        b.add(comp);
        b.add(Box.createGlue());
        return b;
    }

}
